package com.aiti.lulian.service.impl;

import cn.hutool.core.util.ArrayUtil;
import com.aiti.base.core.base.service.iml.BaseSerivceImpl;
import com.aiti.base.core.utils.message.Message;
import com.aiti.base.core.utils.stringUtil.StringUtil;
import com.aiti.lulian.dto.LuLianDockingMeetingPurchaserSignupInformationDto;
import com.aiti.lulian.entity.LuLianDockingMeetingDetails;
import com.aiti.lulian.entity.LuLianDockingMeetingPurchaserPurchaseItem;
import com.aiti.lulian.entity.LuLianDockingMeetingPurchaserSignupInformation;
import com.aiti.lulian.mapper.LuLianDockingMeetingDetailsMapper;
import com.aiti.lulian.mapper.LuLianDockingMeetingPurchaserSignupMapper;
import com.aiti.lulian.service.ILuLianDockingMeetingPurchaserPurchaseItemService;
import com.aiti.lulian.service.ILuLianDockingMeetingPurchaserSignupService;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.ArrayUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @Author WongMZ
 * @CreateDate 2024-07-08
 * @Description: LuLianDockingMeetingPurchaserSignup服务实现类
 */
@Service
public class LuLianDockingMeetingPurchaserSignupServiceImpl extends BaseSerivceImpl<LuLianDockingMeetingPurchaserSignupMapper, LuLianDockingMeetingPurchaserSignupInformation> implements ILuLianDockingMeetingPurchaserSignupService {
    @Autowired
    private LuLianDockingMeetingPurchaserSignupMapper purchaserSignupMapper;

    @Autowired
    private ILuLianDockingMeetingPurchaserPurchaseItemService purchaserPurchaseItemService;

    @Autowired
    private LuLianDockingMeetingDetailsMapper detailsMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addPurchaserSignupInformation(LuLianDockingMeetingPurchaserSignupInformationDto signupInformationDto) {

        LuLianDockingMeetingPurchaserSignupInformation purchaserSignupInformation = new LuLianDockingMeetingPurchaserSignupInformation();
        BeanUtils.copyProperties(signupInformationDto, purchaserSignupInformation);

        //报名的线下会Id
        String meetingId = signupInformationDto.getMeetingId();
        //查询报名线下会是否需要对报名人进行审核
        LuLianDockingMeetingDetails luLianDockingMeetingDetails = detailsMapper.selectById(meetingId);
        //如果是1就不需要审核,将当前报名人的状态设置成已通过审核
        if(StringUtils.equals("1",luLianDockingMeetingDetails.getExamine())){
            purchaserSignupInformation.setStatus("1");
        }
        purchaserSignupInformation.setBaseCreateTime(new Date(System.currentTimeMillis()));
        int res1 = purchaserSignupMapper.insert(purchaserSignupInformation);

        List<LuLianDockingMeetingPurchaserPurchaseItem> purchaseItemList = signupInformationDto.getItemList();
        for (LuLianDockingMeetingPurchaserPurchaseItem purchaseItem : purchaseItemList) {
            purchaseItem.setPurchaserSignupId(purchaserSignupInformation.getBaseId());
            purchaseItem.setCreator(signupInformationDto.getCreator());
        }
        boolean res2 = purchaserPurchaseItemService.saveBatch(purchaseItemList);

        if (res1 > 0 && res2) {
            return 1;
        } else {
            return 0;
        }
    }

    /**
     * 更新采购信息
     *
     * @param signupInformationDto
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateCaiGouDetail(LuLianDockingMeetingPurchaserSignupInformationDto signupInformationDto) {
        LuLianDockingMeetingPurchaserSignupInformation purchaserSignupInformation = new LuLianDockingMeetingPurchaserSignupInformation();
        BeanUtils.copyProperties(signupInformationDto, purchaserSignupInformation);

        purchaserSignupMapper.updateById(purchaserSignupInformation);

        //删除采购商相关的物品
        String baseId = purchaserSignupInformation.getBaseId();
        purchaserPurchaseItemService.remove(new QueryWrapper<LuLianDockingMeetingPurchaserPurchaseItem>().eq("purchaser_signup_id",baseId));

        List<LuLianDockingMeetingPurchaserPurchaseItem> purchaseItemList = signupInformationDto.getItemList();
        for (LuLianDockingMeetingPurchaserPurchaseItem purchaseItem : purchaseItemList) {
            purchaseItem.setPurchaserSignupId(baseId);
        }
        purchaserPurchaseItemService.saveBatch(purchaseItemList);
    }

    @Override
    public LuLianDockingMeetingPurchaserSignupInformation selectInfoById(String id) {
        LuLianDockingMeetingPurchaserSignupInformation detail = purchaserSignupMapper.selectInfoById(id);
        if(detail != null){
            String baseId = detail.getBaseId();
            List<LuLianDockingMeetingPurchaserPurchaseItem> purchaserSignupId = purchaserSignupMapper.selectItemList(baseId);
            detail.setItemList(purchaserSignupId);
        }
        return detail;
    }

    /**
     * 查询采购商列表
     *
     * @param page
     * @param caiGouShang
     * @return
     */
    @Override
    public List<LuLianDockingMeetingPurchaserSignupInformation> queryCaiGouShangBaoMingList(Page<LuLianDockingMeetingPurchaserSignupInformation> page,
                                                                                            LuLianDockingMeetingPurchaserSignupInformationDto caiGouShang,String userId) {
        String roleIds = caiGouShang.getRoleIds();
        if(StringUtil.isNotBlank(roleIds)){
            String[] split = roleIds.split(",");
            if(ArrayUtil.containsAny(split,"4ff234fceaa3498aa4058d9dcc130a6b")){
                userId = "";
            }
        }
        List<LuLianDockingMeetingPurchaserSignupInformation> list = purchaserSignupMapper.findList(page,caiGouShang,userId);
        return list;
    }

    @Override
    public Message checkCaiGouShang(String id, String status, String bhYy) {
        LuLianDockingMeetingPurchaserSignupInformation params = new LuLianDockingMeetingPurchaserSignupInformation();
        params.setBaseId(id);
        params.setStatus(status);
        params.setBhYy(bhYy);
        purchaserSignupMapper.updateById(params);
        return Message.success();
    }

    @Override
    public List<LuLianDockingMeetingDetails> queryPurchaserSignedUpMeetingListPage(Integer pageNum, Integer pageSize, String userId, String activityName) {
        Page<LuLianDockingMeetingPurchaserSignupInformation> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<LuLianDockingMeetingPurchaserSignupInformation> signUpWrapper = new LambdaQueryWrapper<>();
        signUpWrapper.eq(LuLianDockingMeetingPurchaserSignupInformation::getCreator, userId);
        Page<LuLianDockingMeetingPurchaserSignupInformation> signupInformationPage = purchaserSignupMapper.selectPage(page, signUpWrapper);
        if (signupInformationPage.getTotal() > 0) {
            List<LuLianDockingMeetingPurchaserSignupInformation> signupInformationList = signupInformationPage.getRecords();
            List<String> meetingIds = new ArrayList<>();
            for (LuLianDockingMeetingPurchaserSignupInformation information : signupInformationList) {
                meetingIds.add(information.getMeetingId());
            }
            List<LuLianDockingMeetingDetails> list = detailsMapper.findPurchaserSignUpList(meetingIds, activityName, userId);

            return list;
        }
        return new ArrayList<>();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updatePurchaserSignupInformation(LuLianDockingMeetingPurchaserSignupInformationDto signupInformationDto) {
        LuLianDockingMeetingPurchaserSignupInformation purchaserSignupInformation = new LuLianDockingMeetingPurchaserSignupInformation();
        BeanUtils.copyProperties(signupInformationDto, purchaserSignupInformation);

        purchaserSignupInformation.setStatus("0");
        //报名的线下会Id
        String meetingId = signupInformationDto.getMeetingId();
        //查询报名线下会是否需要对报名人进行审核
        LuLianDockingMeetingDetails luLianDockingMeetingDetails = detailsMapper.selectById(meetingId);
        //如果是1就不需要审核,将当前报名人的状态设置成已通过审核
        if(StringUtils.equals("1",luLianDockingMeetingDetails.getExamine())){
            purchaserSignupInformation.setStatus("1");
        }
        purchaserSignupInformation.setBhYy("");
        int res1 = purchaserSignupMapper.updateById(purchaserSignupInformation);

        List<LuLianDockingMeetingPurchaserPurchaseItem> purchaseItemList = signupInformationDto.getItemList();
        LambdaQueryWrapper<LuLianDockingMeetingPurchaserPurchaseItem> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LuLianDockingMeetingPurchaserPurchaseItem::getPurchaserSignupId, purchaserSignupInformation.getBaseId());
        boolean res3 = purchaserPurchaseItemService.remove(wrapper);

        for (LuLianDockingMeetingPurchaserPurchaseItem purchaseItem : purchaseItemList) {
            purchaseItem.setPurchaserSignupId(purchaserSignupInformation.getBaseId());
            purchaseItem.setCreator(signupInformationDto.getCreator());
        }
        boolean res2 = purchaserPurchaseItemService.saveBatch(purchaseItemList);

        if (res1 > 0 && res2 && res3) {
            return 1;
        } else {
            return 0;
        }
    }

    @Override
    public List<LuLianDockingMeetingPurchaserSignupInformation> selectCaiGouShangList(String status,String meetingId) {

        List<LuLianDockingMeetingPurchaserSignupInformation> list = purchaserSignupMapper.selectCustomList(status,meetingId);
        return list;
    }
}